
import HeadPrefab from "../../component/HeadPrefab";
import { ColorConfig } from "../../engine/ColorConfig";
import DateUtil from "../../engine/DateUtil";
import { EngineConfig } from "../../engine/EngineConfig";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { ViewConfig } from "../config/ViewConfig";
import { GameResult } from "../model/GameResult";
import { RoomPlayer } from "../model/RoomPlayer";
import { UserModel } from "../model/UserModel";
import { VideoInfo } from "../model/VideoInfo";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewGameRecord extends ViewBase {

    private tabIndex: number = 0
    private defaultItemNode: cc.Node;

    private gameResultList: Array<GameResult> = []
    private btnFilter: cc.Node;

    private searchId: number = 0

    private btnPre: cc.Node;
    private btnNext: cc.Node;
    private page: number = 1;
    private txtPage: cc.Label;
    private totalPage: number = 1
    private pageSize: number = 3;
    private txtDate: cc.Label
    private btnPreDate: cc.Node;
    private btnNextDate: cc.Node;
    private selectDate: number = DateUtil.now();
    private clickDuiJu: boolean = false

    onEnable(): void {
        super.onEnable()
        this.clickDuiJu = false
        let nowDate: Date = new Date()
        this.selectDate = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate()).getTime()
        this.txtDate = this.getChild(this.node, "txtDate", cc.Label)
        this.btnPreDate = this.findChild(this.node, "btnPreDate")
        this.btnNextDate = this.findChild(this.node, "btnNextDate")
        UIHelper.onAddEvent(this.btnPreDate, () => {
            this.selectDate -= EngineConfig.OneDay;
            this.refreshDate()
            this.getRecord()
        })
        UIHelper.onAddEvent(this.btnNextDate, () => {
            this.selectDate += EngineConfig.OneDay;
            this.refreshDate()
            this.getRecord()
        })

        this.txtPage = this.getChild(this.node, "txtPage", cc.Label)
        this.btnPre = this.findChild(this.node, "btnPre")
        this.btnNext = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(this.btnPre, () => {
            if (this.page > 1) {
                this.page--
                this.refresh()
            }
        })
        UIHelper.onAddEvent(this.btnNext, () => {
            if (this.page < this.totalPage) {
                this.page++;
                this.refresh()
            }
        })

        this.btnFilter = this.findChild(this.node, "btnFilter")
        UIHelper.onAddEvent(this.btnFilter, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewGameRecordFilter, null, (searchId: number) => {
                this.searchId = searchId
                this.getRecord()
            })
        })
        this.defaultItemNode = this.findChild(this.node, "scrollView/view/content/defaultItemNode")
        this.refresh()
        this.getRecord()

        this.refreshDate()
        this.refreshPage()
    }

    private refreshDate() {
        this.txtDate.string = DateUtil.formatDateStr3(this.selectDate)
    }

    private refreshPage() {
        this.totalPage = this.gameResultList.length % this.pageSize == 0 ? this.gameResultList.length / this.pageSize : Math.floor(this.gameResultList.length / this.pageSize) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = "" + this.page + "/" + this.totalPage;
    }

    private getRecord() {
        if (UserModel.instance.nowClub) {
            this.btnFilter.active = true
            Logger.log("getRecord===", this.searchId)
            SocketClient.req_game_record(this.tabIndex + 1, UserModel.instance.nowClub.clubid, this.searchId, this.page, this.pageSize, this.selectDate, (gameResultList: Array<GameResult>) => {
                this.gameResultList = gameResultList;
                this.refresh()
            })
        } else {
            this.btnFilter.active = false
            SocketClient.req_game_record(this.tabIndex + 1, 0, UserModel.instance.userId, this.page, this.pageSize, this.selectDate, (gameResultList: Array<GameResult>) => {
                this.gameResultList = gameResultList;
                this.refresh()
            })
        }
    }

    private refresh() {
        // for (let i = 0; i < GameConfig.Game_Num; i++) {
        //     let btnGame: cc.Node = this.findChild(this.node, "tabNode/btnGame" + i)
        //     if (i == this.tabIndex) {
        //         btnGame.getComponent(cc.Sprite).spriteFrame = btnGame.getComponent(SpriteFrameComponent).spriteFrameList[1]
        //     } else {
        //         btnGame.getComponent(cc.Sprite).spriteFrame = btnGame.getComponent(SpriteFrameComponent).spriteFrameList[0]
        //     }
        //     UIHelper.onAddEvent(btnGame, () => {
        //         this.tabIndex = i
        //         this.page = 1
        //         this.getRecord()
        //         this.refresh()
        //     })
        // }
        this.refreshPage()
        this.unscheduleAllCallbacks()
        this.hideAllChildren(this.defaultItemNode.parent)
        // for (let i = 0; i < this.gameResultList.length; i++) {
        for (let i = 0; i < this.pageSize; i++) {
            let realIndex: number = (this.page - 1) * this.pageSize + i
            let gameResult: GameResult = this.gameResultList[realIndex]
            if (gameResult) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    this.scheduleOnce(() => {
                        this.initOne(gameResult, i)
                    }, 0.02 * i)
                } else {
                    item.active = true;
                    this.initOne(gameResult, i)
                }
            }
        }
    }

    private initOne(gameResult: GameResult, i: number) {
        let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
        if (!itemNode) {
            itemNode = cc.instantiate(this.defaultItemNode)
            itemNode.parent = this.defaultItemNode.parent;
        }
        itemNode.active = true;

        let txtTime: cc.Label = this.getChild(itemNode, "txtTime", cc.Label)
        txtTime.string = DateUtil.formatDateStr(gameResult.time)
        let txtRoomId: cc.Label = this.getChild(itemNode, "txtRoomId", cc.Label)
        txtRoomId.string = "" + gameResult.room_id
        for (let j = 0; j < 4; j++) {
            let txtName: cc.Label = this.getChild(itemNode, "txtName" + j, cc.Label)
            let txtId: cc.Label = this.getChild(itemNode, "txtId" + j, cc.Label)
            let txtScore: cc.Label = this.getChild(itemNode, "txtScore" + j, cc.Label)
            let player: RoomPlayer = gameResult.playerList[j]
            let head: HeadPrefab = this.getChild(itemNode, "head" + j, HeadPrefab)

            if (player) {
                head.node.active = true
                head.setHead(player.headimg, player.lv)
                txtId.string = "ID:" + player.userid
                txtName.string = player.nick_name
                if (player.score >= 0) {
                    txtScore.string = "+" + player.score;
                    txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[0]
                } else {
                    txtScore.string = "-" + player.score;
                    txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[1]
                }
            }
            else {
                head.node.active = false
                txtId.string = ""
                txtName.string = ""
                txtScore.string = ""
            }
        }
        let btnScore: cc.Node = this.findChild(itemNode, "btnScore")
        UIHelper.onAddEvent(btnScore, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewGameResult, null, gameResult, true)
        })
        let btnDuiju: cc.Node = this.findChild(itemNode, "btnDuiju")
        UIHelper.onAddEvent(btnDuiju, () => {
            if (!this.clickDuiJu) {
                this.clickDuiJu = true
                SocketClient.req_game_video(gameResult, (resultList: Array<VideoInfo>) => {
                    this.clickDuiJu = false
                    if (resultList.length > 0) {
                        UIManager.instance.showView(ViewConfig.viewConfig.ViewGameRecordDetail, null, resultList)
                    }
                    else {
                        UIHelper.showTip("没有录像哦")
                    }
                })
            }
        })

    }
}
